<?php


namespace Modules\Common\General\Security\Authorization\Concerns;


use Illuminate\Support\Facades\DB;
use Modules\Common\General\Security\Authorization\Casbin;

trait ManageRolePermission
{
    public function addPermissionsForRole(int $roleId, array $permissionIds, int $branchId)
    {
        DB::transaction(function () use ($roleId, $permissionIds, $branchId) {
            foreach ($permissionIds as $permissionId) {
                Casbin::addPermissionForUser("r$roleId", "p$permissionId", "b$branchId");
            }
        });
        return true;
    }

    public function deletePermissionsForRole(int $roleId, array $permissionIds, int $branchId)
    {
        DB::transaction(function () use ($roleId, $permissionIds, $branchId) {
            foreach ($permissionIds as $permissionId) {
                Casbin::deletePermissionForUser("r$roleId", "p$permissionId", "b$branchId");
            }
        });
    }

    public function deleteAllPermissionsForRole(int $roleId)
    {
        return Casbin::deletePermissionsForUser("r$roleId");
    }

    public function getPermissionsForRole(int $roleId)
    {
        return Casbin::getPermissionsForUser("r$roleId");
    }

    public function getImplicitPermissionsForRole(int $roleId)
    {
        return Casbin::getImplicitPermissionsForUser("r$roleId");
    }


    public function getPermissionsForRoleForPresentation(int $roleId)
    {
        return $this->permissionsForPresentation(Casbin::getPermissionsForUser("r$roleId"));
    }

    public function getImplicitPermissionsForRoleForPresentation(int $roleId)
    {
        return $this->permissionsForPresentation(Casbin::getImplicitPermissionsForUser("r$roleId"));
    }


}
